import { Vec3 } from "cc";
import { Tile } from "./Tile";
import ChessboardState from "./ChessboardState";
import { BlockDirection, GateDirection } from "./enum";



export class LBlockADsorptionEffect {
    /**
     * 模型横轴的长
     */
    width: number = 4;
    /**
     * 模型竖轴的长
     */
    height: number = 4;

    /**
     * 地板横轴的长
     */
    tileWidth: number = 2;

    /**
     * 地板竖轴的长
     */
    tileHeight: number = 2;

    /**
     * 方块的世界坐标
     */
    blockWordPos: Vec3;
    /**
     * 地板列表，为了最后判断算出的坐标拿到对应的地板位置
     */
    checkBoardList: Tile[];
    /**
     * 棋盘右上角
     */
    checkBoardToprightOriginPos: Vec3;
    constructor(
        blockWordPos: Vec3,
        checkBoardList: Tile[],
        checkBoardToprightOriginPos: Vec3,
    ) {
        this.blockWordPos = blockWordPos;
        this.checkBoardList = checkBoardList;
        this.checkBoardToprightOriginPos = checkBoardToprightOriginPos;
    }

    /**
     * 计算方块右上角的位置
     */
    private calcBlockTopRightPos(): Vec3 {
        let topRightPos = new Vec3();
        topRightPos.x = this.blockWordPos.x - (this.width / 2);
        topRightPos.z = this.blockWordPos.z + (this.height / 2);
        return topRightPos;
    }


    /**
     * 
     * 计算出方块右上角和棋盘右上的差值
     */
    private calcBlockTopRightPosDiff(): Vec3 {
        let topRightPos = this.calcBlockTopRightPos();
        let diff = new Vec3();
        diff.x = topRightPos.x - this.checkBoardToprightOriginPos.x;
        diff.z = topRightPos.z - this.checkBoardToprightOriginPos.z;
        return diff;
    }

    /**
     * 计算出对应的地板的下标index
     */
    calcCheckBoardIndex() {
        let diff = this.calcBlockTopRightPosDiff();
        let indexX = Math.round(Math.abs(diff.x / this.tileWidth));
        let indexZ = Math.round(Math.abs(diff.z / this.tileHeight));
        return {
            indexX,
            indexZ
        }
    }

    /**
     * 通过计算出来的地板下标找到对应的地板节点
     */
    private getCheckBoardWordPos() {
        let XZ = this.calcCheckBoardIndex();
        let tileNode = this.checkBoardList.find(item => {
            return (XZ.indexX == item.indexXZ[0] && XZ.indexZ == item.indexXZ[1])
        })
        return tileNode.node.getWorldPosition()
    }
    /** 
     * 计算出对应地板世界坐标的右上角
     */
    private calcCheckBoardWordTileTopRightPos() {
        let wordPos = this.getCheckBoardWordPos();
        let topRigthPos = new Vec3();
        topRigthPos.x = wordPos.x - (this.tileWidth / 2);
        topRigthPos.z = wordPos.z + (this.tileHeight / 2);
        return topRigthPos;
    }

    /**
     * 修正方块位置
     */
    calcBlockWordPos() {
        let topRightPos = this.calcCheckBoardWordTileTopRightPos();
        let worldPos = new Vec3();
        worldPos.x = topRightPos.x + (this.width / 2);
        worldPos.z = topRightPos.z - (this.height / 2);
        worldPos.y = this.blockWordPos.y
        return worldPos;
    }

    calcBlockChekerBoardPos(blockDir: BlockDirection) {
        let XZ = this.calcCheckBoardIndex();

        if (blockDir == BlockDirection.TOP) {
            return [
                {
                    x: XZ.indexX,
                    z: XZ.indexZ,
                },
                {
                    x: XZ.indexX + 1,
                    z: XZ.indexZ,
                },
                {
                    x: XZ.indexX,
                    z: XZ.indexZ + 1,
                },
            ];
        } else if (blockDir == BlockDirection.BOTTOM) {
            return [

                {
                    x: XZ.indexX + 1,
                    z: XZ.indexZ,
                },
                {
                    x: XZ.indexX + 1,
                    z: XZ.indexZ + 1,
                },
                {
                    x: XZ.indexX,
                    z: XZ.indexZ + 1,
                },
            ];
        } else if (blockDir == BlockDirection.LEFT) {
            // debugger;
            return [
                {
                    x: XZ.indexX,
                    z: XZ.indexZ,
                },
                {
                    x: XZ.indexX + 1,
                    z: XZ.indexZ,
                },
                {
                    x: XZ.indexX + 1,
                    z: XZ.indexZ + 1,
                },
            ]
        } else if (blockDir == BlockDirection.RIGHT) {
            // debugger;
            return [
                {
                    x: XZ.indexX,
                    z: XZ.indexZ,
                },
                {
                    x: XZ.indexX,
                    z: XZ.indexZ + 1,
                },
                {
                    x: XZ.indexX + 1,
                    z: XZ.indexZ + 1,
                }
            ]
        }

    }
    calcWhetherBlock(gateDirection: GateDirection, blockDir: BlockDirection): boolean {
        let XZ = this.calcCheckBoardIndex();
        if (gateDirection == GateDirection.TOP) {
            switch (blockDir) {
                case BlockDirection.TOP:
                    return true
                    break;
                case BlockDirection.BOTTOM:
                    if (!ChessboardState.findValue(XZ.indexX, XZ.indexZ)) {
                        return true
                    }
                case BlockDirection.LEFT:
                    return true
                    break;
                case BlockDirection.RIGHT:
                    if (!ChessboardState.findValue(XZ.indexX + 1, XZ.indexZ)) {
                        return true
                    }
                    break;
                default:
                    break;
            }
        }
        if (gateDirection == GateDirection.Bottom) {
            switch (blockDir) {
                case BlockDirection.TOP:
                    if (!ChessboardState.findValue(XZ.indexX, XZ.indexZ)) {
                        return true
                    }
                    break;
                case BlockDirection.BOTTOM:
                    return true
                    break;
                case BlockDirection.LEFT:
                    {
                        if (!ChessboardState.findValue(XZ.indexX, XZ.indexZ + 1)) {
                            return true
                        }
                    }
                    break;
                case BlockDirection.RIGHT:
                    return;
                    break;
                default:
                    break;
            }
        }
        if (gateDirection == GateDirection.LEft) {
            switch (blockDir) {
                case BlockDirection.TOP:
                    return true;
                case BlockDirection.BOTTOM:
                    return true;
                    return false;
                case BlockDirection.LEFT:
                    return true;
                    break;
                case BlockDirection.RIGHT:
                    {
                        if (!ChessboardState.findValue(XZ.indexX + 1, XZ.indexZ)) {
                            return true
                        }
                    }
                    return false;
                default:
                    break;
            }
        }
        if (gateDirection == GateDirection.Right) {
            switch (blockDir) {
                case BlockDirection.TOP:
                    {
                        if (!ChessboardState.findValue(XZ.indexX, XZ.indexZ)) {
                            return true
                        }
                    }
                case BlockDirection.BOTTOM:
                    return true;
                case BlockDirection.LEFT:
                    {
                        if (!ChessboardState.findValue(XZ.indexX, XZ.indexZ + 1)) {
                            return true
                        }
                    }
                case BlockDirection.RIGHT:
                    return true;
                    break;
                default:
                    break;
            }
        }

        return false;
    }
}